package com.js.foodordersystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.js.foodordersystem.entity.Dish;
import com.js.foodordersystem.service.DishService;
import com.js.foodordersystem.util.Result;

import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 存储菜品信息(Dish)表控制层
 *
 * @author makejava
 * @since 2025-01-09 10:52:49
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("dish")
public class DishController {
    /**
     * 服务对象
     */
    @Resource
    private DishService dishService;

    /**
     * 分页查询
     *
     * @param dish  筛选条件
     * @param limit 分页对象
     * @param page  分页对象
     * @return 查询结果
     */
    @GetMapping("findAll")
    public Result findAll(Dish dish, Integer limit, Integer page) {
        if (limit == null || page == null) {
            return Result.failure("limit和page不能为空");
        }
        return dishService.selectAll(dish, limit, page);
    }

    /**
     * 随机八条数据
     * @author 史超杰
     * @date 2025/1/16 16:49
     */
    @GetMapping("getRandom")
    public Result getRandom(@RequestParam(required = false,defaultValue = "8") int limit) {
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.last("ORDER BY RAND()");
        queryWrapper.last("LIMIT " + limit);
        return Result.success(this.dishService.list(queryWrapper));
    }

    /**
     * 根据创建时间，倒序查询
     * @author 史超杰
     * @date 2025/1/16 16:58
     */
    @GetMapping("getByCreatedAt")
    public Result getByCreatedAt(@RequestParam(required = false,defaultValue = "8") int limit) {
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("created_at");
        queryWrapper.last("LIMIT " + limit);
        return Result.success(this.dishService.list(queryWrapper));
    }

    /**
     * 获取爆火菜品
     *
     * @param limit 返回的记录条数
     * @return 爆火菜品列表
     */
    @GetMapping("/getPopular")
    public Result getPopularDishes(@RequestParam(defaultValue = "8") int limit) {
        List<Dish> popularDishes = dishService.getPopularDishes(limit);
        return Result.success(popularDishes);
    }

    /**
     * 根据餐厅id查询菜品
     * @author 史超杰
     * @date 2025/1/17 9:36
     */
    @GetMapping("/getByRestaurantId")
    public Result getByRestaurantId(@RequestParam Long restaurantId) {
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("restaurant_id", restaurantId);
        return Result.success(this.dishService.list(queryWrapper));
    }



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("getById")
    public Result getById(Long id) {
        return Result.success(this.dishService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param dish 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(@RequestBody Dish dish) {
        //判断是否为空
        if (dish == null) {
            return Result.failure("参数不能为空");
        }

        if (this.dishService.save(dish)) {
            return Result.success("添加成功");
        }
        return Result.failure("添加失败");
    }

    /**
     * 编辑数据
     *
     * @param dish 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public Result edit(@RequestBody Dish dish) {
        //判断是否为空
        if (dish == null) {
            return Result.failure("参数不能为空");
        }

        if (this.dishService.updateById(dish)) {
            return Result.success("修改成功");
        }
        return Result.failure("修改失败");
    }

    /**
     * 删除数据
     *
     * @return 删除是否成功
     */
    @PostMapping("delete")
    public Result deleteById(@RequestBody JSONObject data) {
        //判断id是否为空
        Long id = data.getLong("dishId");
        if (id == null) {
            return Result.failure("id不能为空");
        }

        if (this.dishService.removeById(id)) {
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }

}

